#!/bin/sh /etc/rc.common
# OpenWrt 静态ARP绑定 启动脚本
# Copyright (C) 2015 GuoGuo <gch981213@gmail.com>

START=80
STOP=20

USE_PROCD=1

setup_arp() {
	local cfg="$1"
	local enabled ipaddr macaddr ifname

	config_get_bool enabled "$cfg" "enabled" "1"
	[ "$enabled" -eq "1" ] || return 1

	config_get ipaddr "$cfg" "ipaddr"
	config_get macaddr "$cfg" "macaddr"
	config_get ifname "$cfg" "ifname"

	if [ -z "$ipaddr" ] || [ -z "$macaddr" ] || [ -z "$ifname" ]; then
		return 1
	fi

	ip neigh add "$ipaddr" lladdr "$macaddr" nud permanent dev "$ifname" || \
		ip neigh change "$ipaddr" lladdr "$macaddr" nud permanent dev "$ifname"
}

start_service() {
	config_load "arpbind"
	config_foreach setup_arp "arpbind"
}

stop_service() {
	ls "/sys/class/net" | while IFS= read -r ifname; do
		ip link set arp off dev "$ifname"
		ip link set arp on dev "$ifname"
	done
}

reload_service() {
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "arpbind"
}
